#!/bin/bash

DIR=$(dirname "${BASH_SOURCE[0]}")
FILE="GenshinImpact_Data/Plugins/xlua.dll"
sum=($(md5sum $FILE))

if [ "${sum}" != "f91c11ec6ac002d3f11c469f5b003976" ]; then
	# The patch might corrupt invalid/outdated files if this check is skippd.
	echo "Wrong file version or patch is already applied"
	echo "md5sum: ${sum}" && exit 1
fi

if ! command -v xdelta3 &>/dev/null; then
    echo "xdelta3 application is required"
	exit 1
fi

echo "[WARNING] Experimental/temporary patch for 1.2.0"
echo ""

# ===========================================================
echo "[WARNING] Hereby you are violating the game's Terms of Service!"
echo "          Do you accept the risk and possible consequences?"
read -p "Accept? [y/n] " choice

if [[ ! "$choice" == [JjSsYy]* ]]; then
	exit 1
fi

echo "--- Setting up script"
cp -f "$DIR/patch_files/xlua_script.lua" "no message"

echo "--- Applying xLua patch"

# Backup file
mv "$FILE" "$FILE.bak"
xdelta3 -d -s "$FILE.bak" "$DIR/patch_files/xlua_patch.vcdiff" "$FILE"

# Done!
echo "==> Patch applied! Enjoy the game."

exit 0
